package com.example.adapterviewflipperdemo;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterViewFlipper;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private AdapterViewFlipper flipper;
    private int imgIds[] = {R.drawable.baiyang, R.drawable.chunv, R.drawable.jinniu, R.drawable.juxie, R.drawable.mojie, R.drawable.sheshou};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        flipper = findViewById(R.id.flipper);

        BaseAdapter adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return imgIds.length;
            }

            @Override
            public Object getItem(int i) {
                return null;
            }

            @Override
            public long getItemId(int i) {
                return 0;
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                ImageView imgView = new ImageView(MainActivity.this);
                imgView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                imgView.setScaleType(ImageView.ScaleType.FIT_XY);
                imgView.setImageResource(imgIds[i]);
                return imgView;
            }
        };

        flipper.setAdapter(adapter);
    }

    public void prev(View v) {
        flipper.showPrevious();
        flipper.stopFlipping();
    }

    public void next(View v) {
        flipper.showNext();
        flipper.stopFlipping();
    }

    public void auto(View v) {
        flipper.startFlipping();
    }
}
